package com.leezhen.microcloud.config;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.Charset;
import java.util.Base64;

@Configuration
public class RestConfig {

    @Bean
    public HttpHeaders getHeaders() {
        HttpHeaders headers = new HttpHeaders();
        String auth = "java:java";  // 认证的原始信息
        byte[] encodeAuth = Base64.getEncoder()
                .encode(auth.getBytes(Charset.forName("US-ASCII")));    // 进行一个加密处理
        String authHeader = "Basic " + new String(encodeAuth);
        headers.set("Authorization", authHeader);
        return headers;
    }

    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }

}
